package com.atguigu.eduService.controller;


import com.atguigu.common.Result;
import com.atguigu.eduService.client.UcenterClient;
import com.atguigu.eduService.entity.EduComment;
import com.atguigu.eduService.service.EduCommentService;
import com.atguigu.utils.JwtUtils;
import com.atguigu.vo.UcenterMemberVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author smday
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/edu/comment")
//@CrossOrigin
public class EduCommentController {

    @Resource
    private EduCommentService commentService;

    @Resource
    private UcenterClient ucenterClient;


    @ApiOperation("分页查询comment")
    @GetMapping("/page")
    public Result getCommentPage(@RequestParam("current") Long current,
                                 @RequestParam("size") Long size,
                                 @RequestParam String courseId) {

        Page<EduComment> page = new Page<>(current, size);
        Map<String, Object> map = commentService.getCommentPage(page, courseId);
        return Result.ok().data(map);
    }

    @ApiOperation("添加评论")
    @PostMapping
    public Result addComment(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return Result.error().message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberVo info = ucenterClient.getInfoById(memberId);
        comment.setAvatar(info.getAvatar());
        comment.setNickname(info.getNickname());
        commentService.save(comment);
        return Result.ok();
    }

}

